
local LUtil = require 'packages/lingling/utility/_base'

local U = require "packages/utility/utility"

local Global = fk.CreateTriggerSkill{
  name = "#LingGlobal",
  global = true,
  priority = 0.001,

  refresh_events = {fk.AfterCardsMove, fk.AfterDrawPileShuffle, fk.AfterTurnEnd,
    fk.GamePrepared, fk.EventAcquireSkill, fk.EventLoseSkill,
    fk.DrawInitialCards, fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    if player.seat == 1 then
      if event == fk.DrawInitialCards then
        return #player.room.players > 6 and not string.find(player.room.settings.gameMode, "hegemony")
      elseif event == fk.TurnStart then
        return player.room:getTag("RoundCount") == 1 and target.seat > 6 and not string.find(player.room.settings.gameMode, "hegemony")
      else
        if player.room:getBanner("@$CenterArea") ~= nil then
          if event == fk.AfterCardsMove or event == fk.AfterDrawPileShuffle or event == fk.AfterTurnEnd then
            return true
          --[[elseif event == fk.EventLoseSkill then
            return data.CenterArea and table.every(player.room.alive_players, function (p)
              return not table.find(p.player_skills, function (s)
                return s.CenterArea
              end)
            end)]]--
          end
        else
          if event == fk.GamePrepared then
            --[[return table.find(player.room.alive_players, function (p)
              return table.find(p.player_skills, function (s)
                return s.CenterArea
              end)
            end)]]--
            return true
          elseif event == fk.EventAcquireSkill then
            --return data.CenterArea
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    --七八号位增益
    if event == fk.DrawInitialCards then
      if target.seat == 7 then
        data.num = data.num + 1
      elseif target.seat == 8 then
        data.num = data.num + 2
      end
    elseif event == fk.TurnStart then
      room:handleAddLoseSkills(target, "m_feiyang", nil, false, true)
      room.logic:getCurrentEvent():findParent(GameEvent.Turn, true):addCleaner(function()
        room:handleAddLoseSkills(target, "-m_feiyang", nil, false, true)
      end)
    else
      --雌雄双股剑替换
      if event == fk.GamePrepared then
        for i = #room.draw_pile, 1, -1 do
          local id = room.draw_pile[i]
          if Fk:getCardById(id).name == "double_swords" then
            table.insert(room.void, id)
            room:setCardArea(id, Card.Void, nil)
            local new_card = room:printCard("ling__double_swords", Fk:getCardById(id).suit, Fk:getCardById(id).number)
            room.draw_pile[i] = new_card.id
            room:setCardArea(new_card.id, Card.DrawPile, nil)
          end
        end
      end
      --中央区显示相关
      if event == fk.EventLoseSkill then
        room:setBanner("@$CenterArea", nil)
      elseif event == fk.AfterTurnEnd then
        room:setBanner("@$CenterArea", {})
      else
        local cards = {}
        room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.toArea == Card.DiscardPile then
              for _, info in ipairs(move.moveInfo) do
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end, Player.HistoryTurn)
        cards = table.filter(cards, function (id)
          if table.contains(room.discard_pile, id) then
            local card = Fk:getCardById(id)
            for _, suffix in ipairs(MarkEnum.TempMarkSuffix) do
              if card:getMark("RemoveCenterCards"..suffix) == 0 then
                return true
              end
            end
          end
        end)
        room:setBanner("@$CenterArea", cards)
      end
    end
  end,
}
Fk:addSkill(Global)
Fk:loadTranslationTable{
  ["@$CenterArea"] = "中央区",
  ["$CenterArea"] = "中央区",
}

return LUtil
